package com.khshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khshop.pojo.Product;
import com.khshop.mapper.ProductMapper;
import com.khshop.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.khshop.vo.ServiceResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * <p>
 * 商品 服务实现类
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-14
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired(required = false)
    ProductMapper productMapper;

    @Override
    public ServiceResult<Product> addProduct(Product product) {
        return ServiceResult.of(productMapper.insert(product)>0);
    }

    @Override
    public ServiceResult<Product> addProductStock(Long id, int num) {
        Product product = productMapper.selectById(id);
        product.setStockcount(product.getStockcount()+num);
        return ServiceResult.of(productMapper.updateById(product)>0);
    }

    @Override
    public ServiceResult<Product> updateProduct(Product product) {
        return ServiceResult.of(productMapper.updateById(product)>0);
    }

    @Override
    public ServiceResult<Product> delProduct(Long id) {
        Product product = productMapper.selectById(id);
        product.setInuse(false);
        return ServiceResult.of(productMapper.updateById(product)>0);
    }

    @Override
    public ServiceResult<IPage<Product>> searchProduct(QueryWrapper<Product> queryWrapper, Integer page, Integer limit) {
        Page<Product> iPage = new Page<Product>(page, limit);
        IPage<Product> productIPage = productMapper.selectPage(iPage, queryWrapper);

        return ServiceResult.of(productIPage,productIPage.getSize()>0);
    }

    @Override
    public ServiceResult<Product> onsaleProduct(Long[] id, boolean status) {
        Product product = new Product();
        product.setInuse(status);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<Product>();
        queryWrapper.in("id",Arrays.asList(id));
        return ServiceResult.of(productMapper.update(product,queryWrapper)>0);
    }

    @Override
    public ServiceResult<Product> productSort() {
        return null;
    }
}
